package edu.pku.sei.transformation.editor.model.base;

import java.util.Collection;
import java.util.HashMap;

public class HashNameTable implements INameSpace{
	private HashMap<String, NamedElement> nameMap = new HashMap<String, NamedElement>();
	
	@Override
	public Collection<NamedElement> getAllElements() {
		return nameMap.values();
	}

	@Override
	public NamedElement getElement(String name) {
		return nameMap.get(name);
	}

	@Override
	public boolean registerName(NamedElement element) {
		String name = element.getName();
		if (nameMap.get(name) == null) {
			nameMap.put(name, element);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean unregisterName(NamedElement element) {
		return unregisterName(element.getName());
	}

	@Override
	public boolean unregisterName(String name) {
		if (nameMap.remove(name) == null)
			return false;
		else
			return true;
	}
}
